<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Acceso extends CI_Controller {

       public function index() {
        $this->sesion();
        $this->load->model("reserva_model");
        $data['registros3'] = $this->reserva_model->NotificacionesReserva();
        $data['registros4'] = $this->reserva_model->NotificacionesReservaHOY();
        $this->load->view('intranet/intranet_view',$data);
        $this->load->view('estructura/footer');
    }

    public function cancelarReserva($codigo) {
        if (isset($this->session->userdata['nombre'])) {
            if (isset($codigo)) {
                $this->load->model('reserva_model');
                
                $data = array(
                    'est_res' => 2
                );
                $this->reserva_model->CancelarReserva($codigo, $data);
            }
            $this->index();
        } else {
            $this->login();
        }
    }

    public function login() {
        $this->load->view('login');
    }

public function validar() {
        if (isset($_POST['usu'])) {
            $this->form_validation->set_rules('usu', 'Usuario', 'required|min_lenght[5]|max_lenght[20]');
            $this->form_validation->set_rules('pass', 'Clave', 'required');

            if ($this->form_validation->run() == FALSE) {
                $this->login();
            } else {
                $usuario = $_POST['usu'];
                $password = $_POST['pass'];
                $this->load->model("usuario_model");
                $result = $this->usuario_model->logeo_personal($usuario, $password);

                if ($result) {
                    $sesion_data = array(
                        'codigo' => $result{0}['id_pers'],
                        'nombre' => $result{0}['nom_per'],
                        'apellido' => $result{0}['ape_per'],
                        'direccion' => $result{0}['dir_per'],
                        'usuario' => $usuario,
                        'clave' => $password
                    );
                    $this->session->set_userdata($sesion_data);
                   
                    $this->index();
                    
                } else {
                    $derror['error'] = "Usuario o clave incorrecta, por favor vuelva a intentarlo";
                    $this->load->view('login', $derror);
                }
            }
        } else {
            if (isset($this->session->userdata['nombre'])) {
                
                $this->index();
              
            } else {
                $this->login();
            }
        }
    }

    public function sesion() {
        $data['nom'] = $this->session->userdata['nombre'];
        $data['ape'] = $this->session->userdata['apellido'];
        $data['ruta'] = '<li><i class="icon-home home-icon"></i> Home</li>';
        $data['css'] = array('inicio' => 'active', 'mantenedor' => '', 'reserva' => '');
        $this->load->view('estructura/head', $data);
    }

    public function cerrarSesion() {
        $this->session->sess_destroy();
        $this->login();
    }

}